package com.jhws.core.db;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate4.HibernateTemplate;

public class DaoTemplateContext implements ApplicationContextAware{

	private static ApplicationContext context = null;
	
	private static final ThreadLocal<JdbcTemplate> jt = new ThreadLocal<JdbcTemplate>(){
		protected JdbcTemplate initialValue() {
			return context.getBean("j", JdbcTemplate.class);
		};
	};
	private static final ThreadLocal<HibernateTemplate> ht = new ThreadLocal<HibernateTemplate>(){
		protected HibernateTemplate initialValue() {
			return context.getBean("h", HibernateTemplate.class);
		};
	};
	
	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		context = applicationContext;
	}
	
	public static JdbcTemplate getJdbcTemplate(){
		return jt.get();
	}
	
	public static HibernateTemplate getHibernateTemplate(){
		return ht.get();
	}
	
	public static void destory(){
		jt.remove();
		ht.remove();
	}

}
